import { createRouter, createWebHashHistory } from 'vue-router'

import Home from './Home.vue'
import Main from './Main.vue'
import Login from './Login.vue'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        { path: '/', redirect: '/home' },
        { path: '/home', component: Home },
        { path: '/main', component: Main },
        { path: '/login', component: Login }
    ]

})
router.beforeEach((to, from, next) => {
    // console.log(to);
    // console.log(from);
    // next()
    // 通过这个方法获得token值
    const tokenStr = localStorage.getItem('token')
    if (to.path === '/main' && !tokenStr) {
        // 证明用户要访问后台页面
        next('/login')
    } else {
        // 访问的不是后台页面
        next()
    }
})

export default router